home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / WINDOWS / GVECTS.ZIP / HDIB.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-02  |  2.2 KB  |  97 lines

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "hdib.h"
  5.  
  6. int HDIB::Load(char *file)
  7. {
  8.     FILE    *fp;
  9.     int        NumColors;
  10.     BYTE    *TempData;
  11.     char    *d;
  12.     int        i, j;
  13.     BYTE    Temp;
  14.     int        Rem;
  15.         
  16.     fp = fopen(file, "rb");
  17.     if(!fp)
  18.     {
  19.         MessageBeep(0);
  20.         fclose(fp);
  21.         return 0;
  22.     }
  23.  
  24. // This will be the bitmap's info
  25.     fseek(fp, sizeof(BITMAPFILEHEADER), SEEK_SET);
  26.     fread(&Info, sizeof(BITMAPINFOHEADER), 1, fp);
  27.     Rem = 4 - Info.biWidth & 3;
  28.     Info.biSizeImage = ((DWORD)(UINT)Info.biWidth * (DWORD)(UINT)Info.biHeight);
  29.     Data = (BYTE *)GlobalAlloc(GPTR, Info.biSizeImage);
  30.     TempData = new BYTE[Info.biSizeImage];
  31.     d = (char *)Data;
  32.  
  33. // This should be the palette for the bitmap
  34.     NumColors = (Info.biClrUsed == 0 ? 256 : (int)Info.biClrUsed);
  35.     fread(Palette, sizeof(RGBQUAD), NumColors, fp);
  36.     for(i = 0; i < 256; i++)
  37.     {
  38.         Temp = Palette[i].peRed;
  39.         Palette[i].peRed = Palette[i].peBlue;
  40.         Palette[i].peBlue = Temp;
  41.     }
  42.     Info.biClrUsed = NumColors;
  43.  
  44. // And this will be the rest of the bitmap data
  45.     for(i = 0; i < Info.biHeight; i++)
  46.     {
  47.         fread((BYTE *)&TempData[i * Info.biWidth], sizeof(char), (int)Info.biWidth, fp);
  48.         for(j = 0; j < Rem; j++)
  49.             fgetc(fp);
  50.     }
  51.  
  52.     for(long s = Info.biSizeImage - 1; s >= 0; s--)
  53.         *(d + s) = TempData[Info.biSizeImage - 1 - s];
  54.     delete TempData;
  55.  
  56.     TempData = new BYTE[Info.biWidth];
  57.     for(long h = 0; h < Info.biHeight; h++)
  58.     {
  59.         for(long w = 0; w < Info.biWidth; w++)
  60.             TempData[w] = *(d + h * Info.biWidth + Info.biWidth - w - 1);
  61.         for(w = 0; w < Info.biWidth; w++)
  62.             *(d + h * Info.biWidth + w) = TempData[w];
  63.     }
  64.     delete TempData;
  65.  
  66.     fclose(fp);
  67.     return 0;
  68. };
  69.  
  70. int HDIB::Free()
  71. {
  72.     GlobalFree((BYTE *)Data);
  73.     return 0;
  74. };
  75.  
  76. HPALETTE HDIB::ReturnPalette()
  77. {
  78.     LOGPALETTE    *LogPal;
  79.     HPALETTE    Pal;
  80.     int            i;
  81.     
  82.     LogPal = (LOGPALETTE *)LocalAlloc(LPTR, sizeof(LOGPALETTE) + 236 * sizeof(PALETTEENTRY));
  83.     LogPal->palNumEntries = 236;
  84.     LogPal->palVersion = 0x300;
  85.     
  86.     for(i = 0; i < 236; i++)
  87.     {
  88.         LogPal->palPalEntry[i] = Palette[i + 10];
  89.         LogPal->palPalEntry[i].peFlags = PC_RESERVED;
  90.     }
  91.     
  92.     Pal = CreatePalette((LOGPALETTE *)LogPal);
  93.     LocalFree((LOGPALETTE *)LogPal);
  94.     return(Pal);
  95. };
  96.  
  97.